home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 3.iso / dist / fw_apache.idb / usr / freeware / apache / bin / dbmmanage.z / dbmmanage
Text File  |  2001-01-10  |  11KB  |  357 lines

  1. #!/usr/bin/perl5
  2.  
  3. # ====================================================================
  4. # The Apache Software License, Version 1.1
  5. #
  6. # Copyright (c) 2000 The Apache Software Foundation.  All rights
  7. # reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright
  14. #    notice, this list of conditions and the following disclaimer.
  15. #
  16. # 2. Redistributions in binary form must reproduce the above copyright
  17. #    notice, this list of conditions and the following disclaimer in
  18. #    the documentation and/or other materials provided with the
  19. #    distribution.
  20. #
  21. # 3. The end-user documentation included with the redistribution,
  22. #    if any, must include the following acknowledgment:
  23. #       "This product includes software developed by the
  24. #        Apache Software Foundation (http://www.apache.org/)."
  25. #    Alternately, this acknowledgment may appear in the software itself,
  26. #    if and wherever such third-party acknowledgments normally appear.
  27. #
  28. # 4. The names "Apache" and "Apache Software Foundation" must
  29. #    not be used to endorse or promote products derived from this
  30. #    software without prior written permission. For written
  31. #    permission, please contact apache@apache.org.
  32. #
  33. # 5. Products derived from this software may not be called "Apache",
  34. #    nor may "Apache" appear in their name, without prior written
  35. #    permission of the Apache Software Foundation.
  36. #
  37. # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  38. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  39. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. # DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  41. # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  43. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  44. # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  45. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  47. # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48. # SUCH DAMAGE.
  49. # ====================================================================
  50. # ====================================================================
  51. #
  52. # This software consists of voluntary contributions made by many
  53. # individuals on behalf of the Apache Software Foundation.  For more
  54. # information on the Apache Software Foundation, please see
  55. # <http://www.apache.org/>.
  56. #
  57. # Portions of this software are based upon public domain software
  58. # originally written at the National Center for Supercomputing Applications,
  59. # University of Illinois, Urbana-Champaign.
  60. #
  61.  
  62. #for more functionality see the HTTPD::UserAdmin module:
  63. # http://www.perl.com/CPAN/modules/by-module/HTTPD/HTTPD-Tools-x.xx.tar.gz
  64. #
  65. # usage: dbmmanage <DBMfile> <command> <user> <password> <groups> <comment>
  66.  
  67. package dbmmanage;
  68. #                               -ldb    -lndbm    -lgdbm    -lsdbm
  69. BEGIN { @AnyDBM_File::ISA = qw(DB_File NDBM_File GDBM_File SDBM_File) }
  70. use strict;
  71. use Fcntl;
  72. use AnyDBM_File ();
  73.  
  74. sub usage {
  75.     my $cmds = join "|", sort keys %dbmc::;
  76.     die <<SYNTAX;
  77. Usage: dbmmanage [enc] dbname command [username [pw [group[,group] [comment]]]]
  78.  
  79.     where enc is  -d for crypt encryption (default except on Win32, Netware)
  80.                   -m for MD5 encryption (default on Win32, Netware)
  81.                   -s for SHA1 encryption
  82.                   -p for plaintext
  83.  
  84.     command is one of: $cmds
  85.  
  86.     pw of . for update command retains the old password
  87.     pw of - (or blank) for update command prompts for the password
  88.  
  89.     groups or comment of . (or blank) for update command retains old values
  90.     groups or comment of - for update command clears the existing value
  91.     groups or comment of - for add and adduser commands is the empty value
  92. SYNTAX
  93. }
  94.  
  95. sub need_sha1_crypt {
  96.     if (!eval ('require "Digest/SHA1.pm";')) {
  97.         print STDERR <<SHAERR;
  98. dbmmanage SHA1 passwords require the interface or the module Digest::SHA1
  99. available from CPAN:
  100.  
  101.     http://www.cpan.org/modules/by-module/Digest/Digest-MD5-2.12.tar.gz
  102.  
  103. Please install Digest::SHA1 and try again, or use a different crypt option:
  104.  
  105. SHAERR
  106.         usage();
  107.     }
  108. }
  109.  
  110. sub need_md5_crypt {
  111.     if (!eval ('require "Crypt/PasswdMD5.pm";')) {
  112.         print STDERR <<MD5ERR;
  113. dbmmanage MD5 passwords require the module Crypt::PasswdMD5 available from CPAN
  114.  
  115.     http://www.cpan.org/modules/by-module/Crypt/PasswdMD5-1.1.tar.gz
  116.  
  117. Please install Crypt::PasswdMD5 and try again, or use a different crypt option:
  118.  
  119. MD5ERR
  120.         usage();
  121.     }
  122. }
  123.  
  124. # if your osname is in $newstyle_salt, then use new style salt (starts with '_' and contains
  125. # four bytes of iteration count and four bytes of salt).  Otherwise, just use
  126. # the traditional two-byte salt.
  127. # see the man page on your system to decide if you have a newer crypt() lib.
  128. # I believe that 4.4BSD derived systems do (at least BSD/OS 2.0 does).
  129. # The new style crypt() allows up to 20 characters of the password to be
  130. # significant rather than only 8.
  131. #
  132. my $newstyle_salt_platforms = join '|', qw{bsdos}; #others?
  133. my $newstyle_salt = $^O =~ /(?:$newstyle_salt_platforms)/;
  134.  
  135. # Some platforms just can't crypt() for Apache
  136. #
  137. my $crypt_not_supported_platforms = join '|', qw{MSWin32 NetWare}; #others?
  138. my $crypt_not_supported = $^O =~ /(?:$crypt_not_supported_platforms)/;
  139.  
  140. my $crypt_method = "crypt";
  141.  
  142. if ($crypt_not_supported) {
  143.     $crypt_method = "md5";
  144. }
  145.  
  146. # Some platforms won't jump through our favorite hoops
  147. #
  148. my $not_unix_platforms = join '|', qw{MSWin32 NetWare}; #others?
  149. my $not_unix = $^O =~ /(?:$not_unix_platforms)/;
  150.  
  151. if ($crypt_not_supported) {
  152.     $crypt_method = "md5";
  153. }
  154.  
  155. if (@ARGV[0] eq "-d") {
  156.     shift @ARGV;
  157.     if ($crypt_not_supported) {
  158.         print STDERR 
  159.               "Warning: Apache/$^O does not support crypt()ed passwords!\n\n";
  160.     }
  161.     $crypt_method = "crypt";
  162. }
  163.  
  164. if (@ARGV[0] eq "-m") {
  165.     shift @ARGV;
  166.     $crypt_method = "md5";
  167. }
  168.  
  169. if (@ARGV[0] eq "-p") {
  170.     shift @ARGV;
  171.     if (!$crypt_not_supported) {
  172.         print STDERR 
  173.               "Warning: Apache/$^O does not support plaintext passwords!\n\n";
  174.     }
  175.     $crypt_method = "plain";
  176. }
  177.  
  178. if (@ARGV[0] eq "-s") {
  179.     shift @ARGV;
  180.     need_sha1_crypt();
  181.     $crypt_method = "sha1";
  182. }
  183.  
  184. if ($crypt_method eq "md5") {
  185.     need_md5_crypt();
  186. }
  187.  
  188. my($file,$command,$key,$crypted_pwd,$groups,$comment) = @ARGV;
  189.  
  190. usage() unless $file and $command and defined &{$dbmc::{$command}};
  191.  
  192. # remove extension if any
  193. my $chop = join '|', qw{db.? pag dir};
  194. $file =~ s/\.($chop)$//;
  195.  
  196. my $is_update = $command eq "update";
  197. my %DB = ();
  198. my @range = ();
  199. my($mode, $flags) = $command =~ 
  200.     /^(?:view|check)$/ ? (0644, O_RDONLY) : (0644, O_RDWR|O_CREAT);
  201.  
  202. tie (%DB, "AnyDBM_File", $file, $flags, $mode) || die "Can't tie $file: $!";
  203. dbmc->$command();
  204. untie %DB;
  205.  
  206.  
  207. my $x;
  208. sub genseed {
  209.     my $psf;
  210.     if ($not_unix) {
  211.     srand (time ^ $$ or time ^ ($$ + ($$ << 15)));
  212.     }
  213.     else {
  214.         for (qw(-xlwwa -le)) { 
  215.         `ps $_ 2>/dev/null`;
  216.             $psf = $_, last unless $?;
  217.         }
  218.         srand (time ^ $$ ^ unpack("%L*", `ps $psf | gzip -f`));
  219.     }
  220.     @range = (qw(. /), '0'..'9','a'..'z','A'..'Z');
  221.     $x = int scalar @range;
  222. }
  223.  
  224. sub randchar { 
  225.     join '', map $range[rand $x], 1..shift||1;
  226. }
  227.  
  228. sub saltpw_crypt {
  229.     genseed() unless @range; 
  230.     return $newstyle_salt ? 
  231.     join '', "_", randchar, "a..", randchar(4) :
  232.         randchar(2);
  233. }
  234.  
  235. sub cryptpw_crypt {
  236.     my ($pw, $salt) = @_;
  237.     $salt = saltpw_crypt unless $salt;
  238.     crypt $pw, $salt;
  239. }
  240.  
  241. sub saltpw_md5 {
  242.     genseed() unless @range; 
  243.     randchar(8);
  244. }
  245.  
  246. sub cryptpw_md5 {
  247.     my($pw, $salt) = @_;
  248.     $salt = saltpw_md5 unless $salt;
  249.     Crypt::PasswdMD5::apache_md5_crypt($pw, $salt);
  250. }
  251.  
  252. sub cryptpw_sha1 {
  253.     my($pw, $salt) = @_;
  254.     '{SHA}' . Digest::SHA1::sha1_base64($pw) . "=";
  255. }
  256.  
  257. sub cryptpw {
  258.     if ($crypt_method eq "md5") {
  259.         return cryptpw_md5(@_);
  260.     } elsif ($crypt_method eq "sha1") {
  261.         return cryptpw_sha1(@_);
  262.     } elsif ($crypt_method eq "crypt") {
  263.         return cryptpw_crypt(@_);
  264.     }
  265.     @_[0]; # otherwise return plaintext
  266. }
  267.  
  268. sub getpass {
  269.     my $prompt = shift || "Enter password:";
  270.  
  271.     unless($not_unix) { 
  272.     open STDIN, "/dev/tty" or warn "couldn't open /dev/tty $!\n";
  273.     system "stty -echo;";
  274.     }
  275.  
  276.     my($c,$pwd);
  277.     print STDERR $prompt;
  278.     while (($c = getc(STDIN)) ne '' and $c ne "\n" and $c ne "\r") {
  279.     $pwd .= $c;
  280.     }
  281.  
  282.     system "stty echo" unless $not_unix;
  283.     print STDERR "\n";
  284.     die "Can't use empty password!\n" unless length $pwd;
  285.     return $pwd;
  286. }
  287.  
  288. sub dbmc::update {
  289.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  290.     $crypted_pwd = (split /:/, $DB{$key}, 3)[0] if $crypted_pwd eq '.';
  291.     $groups = (split /:/, $DB{$key}, 3)[1] if !$groups || $groups eq '.';
  292.     $comment = (split /:/, $DB{$key}, 3)[2] if !$comment || $comment eq '.';
  293.     if (!$crypted_pwd || $crypted_pwd eq '-') {
  294.         dbmc->adduser;
  295.     }
  296.     else {
  297.         dbmc->add;
  298.     }
  299. }
  300.  
  301. sub dbmc::add {
  302.     die "Can't use empty password!\n" unless $crypted_pwd;
  303.     unless($is_update) {
  304.     die "Sorry, user `$key' already exists!\n" if $DB{$key};
  305.     }
  306.     $groups = '' if $groups eq '-';
  307.     $comment = '' if $comment eq '-';
  308.     $groups .= ":" . $comment if $comment;
  309.     $crypted_pwd .= ":" . $groups if $groups;
  310.     $DB{$key} = $crypted_pwd;
  311.     my $action = $is_update ? "updated" : "added";
  312.     print "User $key $action with password encrypted to $DB{$key} using $crypt_method\n";
  313. }
  314.  
  315. sub dbmc::adduser {
  316.     my $value = getpass "New password:";
  317.     die "They don't match, sorry.\n" unless getpass("Re-type new password:") eq $value;
  318.     $crypted_pwd = cryptpw $value;
  319.     dbmc->add;
  320. }
  321.  
  322. sub dbmc::delete {
  323.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  324.     delete $DB{$key}, print "`$key' deleted\n";
  325. }
  326.  
  327. sub dbmc::view {
  328.     print $key ? "$key:$DB{$key}\n" : map { "$_:$DB{$_}\n" if $DB{$_} } keys %DB;
  329. }
  330.  
  331. sub dbmc::check {
  332.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  333.     my $chkpass = (split /:/, $DB{$key}, 3)[0];
  334.     my $testpass = getpass();
  335.     if (substr($chkpass, 0, 6) eq '$apr1$') {
  336.         need_md5_crypt;
  337.         $crypt_method = "md5";
  338.     } elsif (substr($chkpass, 0, 5) eq '{SHA}') {
  339.         need_sha1_crypt;
  340.         $crypt_method = "sha1";
  341.     } elsif (length($chkpass) == 13 && $chkpass ne $testpass) {
  342.         $crypt_method = "crypt";
  343.     } else {
  344.         $crypt_method = "plain";
  345.     }
  346.     print $crypt_method . (cryptpw($testpass, $chkpass) eq $chkpass 
  347.                            ? " password ok\n" : " password mismatch\n");
  348. }
  349.  
  350. sub dbmc::import {
  351.     while(defined($_ = <STDIN>) and chomp) {
  352.     ($key,$crypted_pwd,$groups,$comment) = split /:/, $_, 4;
  353.     dbmc->add;
  354.     }
  355. }
  356.  
  357.